﻿using LJX.SmartPark.Common.Extensions;
using LJX.SmartPark.Common.ResponseOutput;
using LJX.SmartPark.Domain.SysManage;
using LJX.SmartPark.Service.Repository;
using LJX.SmartPark.Service.SysManage.Menu.Dto;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LJX.SmartPark.Service.SysManage.Menu
{
    public class MenuManageService : IMenuManageService
    {
        private readonly IRepository<MenuManage> _studentService;

        public MenuManageService(IRepository<MenuManage> studentService)
        {
            _studentService = studentService;
        }

        #region 

        public async Task<Response<List<MenuManage>>> GetMenuManageAsync()
        {
            var students = await _studentService.GetAllListAsync();
            var result = students.MapToList<MenuManage>();

            return Response<List<MenuManage>>.Ok(result);
        }

        public async Task<Response<bool>> PutMenuManage(MenuManage input)
        {
            var result = _studentService.UpdateAsync(input.MapTo<MenuManage>());

            return Response<bool>.Ok(result.Result > 0);
        }

        public object GrtTreeMenuManage(int id = 0)
        {
            var students = _studentService.GetListQuery().Where(x => x.PId == id).ToList();
            if (students.Count == 0)
            {
                return null;
            }
            var list = students.Select(a => new
            {
                id = a.Id,
                label = a.Name,
                children = GrtTreeMenuManage(a.Id)
            }).ToList();

            return list;
        }

        #endregion

        /// <summary>
        /// 获取菜单管理列表
        /// </summary>
        /// <returns></returns>
        public async Task<Response<List<MenuManageDto>>> GetMenuManage()
        {
            var menuList = _studentService.GetListQuery();
            //查询出=父级菜单
            var parentMenu = menuList.Where(m => m.PId == 0).ToList();

            List<MenuManageDto> manageOuts = new List<MenuManageDto>();

            foreach (var menu in parentMenu)
            {
                manageOuts.Add(new MenuManageDto
                {
                    Id = menu.Id,
                    PId = menu.PId,
                    Name = menu.Name,
                    Icon = menu.Icon,
                    Path = menu.Path,
                    SortId = menu.SortId,
                    Disable = menu.Disable,
                    Children = GetChildenMenu(menu.Id, menuList.MapToList<MenuManageDto>())
                });
            };
            return Response<List<MenuManageDto>>.Ok(manageOuts);
        }


        /// <summary>
        /// 递归
        /// </summary>
        /// <param name="pid"></param>
        /// <param name="menus"></param>
        /// <returns></returns>
        public List<MenuManageDto> GetChildenMenu(int pid, List<MenuManageDto> menus)
        {
            List<MenuManageDto> menuManages = new List<MenuManageDto>();
            var parentMenu = menus.Where(m => m.PId == pid).ToList();
            if (!parentMenu.Any())
            {
                return menuManages;
            }
            foreach (var menu in parentMenu)
            {
                menuManages.Add(new MenuManageDto
                {
                    Id = menu.Id,
                    PId = menu.PId,
                    Name = menu.Name,
                    Icon = menu.Icon,
                    Path = menu.Path,
                    SortId = menu.SortId,
                    Disable = menu.Disable,
                    Children = GetChildenMenu(menu.Id, menus)
                });
            }
            return menuManages;
        }

        /// <summary>
        /// 获取列表
        /// </summary>
        /// <returns></returns>
        public async Task<Response<List<MenuManage>>> GetMenuManages()
        {
            var menuList = _studentService.GetListQuery();
            var parentMenu = menuList.MapToList<MenuManage>();

            return Response<List<MenuManage>>.Ok(parentMenu);
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<Response<int>> AddMenuManageAsync(MenuManage input)
        {
            var obj = await _studentService.InsertAsyncById(input.MapTo<MenuManage>());
            return Response<int>.Ok(obj);
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<Response<int>> UpdateMenuManageAsync(MenuManage input)
        {
            var obj = await _studentService.UpdateAsync(input.MapTo<MenuManage>());
            return Response<int>.Ok(obj);
        }

    }
}
